package com.jdls.day11.work;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;
import java.util.TreeMap;

/**
 * @author 嘉德罗斯
 * @date 2021/7/31
 */
public class Test5 {
    public static void main(String[] args) throws IOException {
        // 定义任意字符串数组
        //    统计该数组中所有字符出现的次数，将统计结果输出到当前模块下 count.txt文件中
        //    提示： 在输出的内容后面加入：\r\n 就有换行效果
        String[] str = {"hello","world","java","heiheihei"};
        TreeMap<Character, Integer> tree = new TreeMap<>();
        for (String s : str) {
            char[] cs = s.toCharArray();
            for (char c : cs) {
                if(tree.containsKey(c)){
                    int a = tree.get(c);
                    tree.put(c,a+1);
                }else {
                    tree.put(c,1);
                }
            }
        }
        FileOutputStream f1 = new FileOutputStream("jdls-class\\count.txt");
        for (Map.Entry<Character, Integer> s : tree.entrySet()) {
            f1.write((s.getKey()+":"+s.getValue()+"\r\n").getBytes());
            System.out.println(s);
        }
        f1.close();
    }
}
